package site.xuxingcan.wifihost;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import java.io.DataOutputStream;

/**
 * Created by xuxingcan on 17-1-9.
 */

public class NotifyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle("后台服务")//设置通知栏标题
                .setContentText("请勿删除") //设置通知栏显示内容
//  .setNumber(number) //设置通知集合的数量
//                .setTicker("测试通知来啦") //通知首次出现在通知栏，带上升动画效果的
                .setPriority(Notification.PRIORITY_MAX) //设置该通知优先级
//  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
                .setOngoing(true)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
                .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
                //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
                .setSmallIcon(R.mipmap.icon);//设置通知小ICON
        mNotificationManager.notify(160101, mBuilder.build());

    }

    private void refresh() {
        long bootTime = System.currentTimeMillis() - SystemClock.elapsedRealtime();
        long lastClickTime = SharedPreferenceUtil.getInstance(this).getLastClickTime();
        if (bootTime < lastClickTime) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
//                        exec(SharedPreferenceUtil.getInstance(getApplicationContext()).getQuanxianPath());
                        exec(SharedPreferenceUtil.getInstance(getApplicationContext()).getRefreshPath());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent == null) {
            refresh();
        } else {
            final String cmd = intent.getStringExtra("CMD");
            if (TextUtils.isEmpty(cmd)) {
                refresh();
            } else {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            exec(cmd);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    public static void exec(String paramString) throws Exception {
        Process localProcess = Runtime.getRuntime().exec("su");
        DataOutputStream localDataOutputStream = new DataOutputStream(localProcess.getOutputStream());
        localDataOutputStream.writeBytes(paramString + "\n");
//        localDataOutputStream.writeBytes("exit\n");
        localDataOutputStream.flush();
//        localDataOutputStream.close();
        localProcess.waitFor();
//        localProcess.destroy();
    }
}
